<template>
  <div>
    <van-nav-bar
      :title="`${$route.query.key}的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        :to="`/article/${item.art_id}`"
      />
    </van-list>
  </div>
</template>

<script>
import { resultsAPI } from '@/api/search'
export default {
  name: 'SearchResults',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await resultsAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.key
      })
      //   console.log('请求结果', res)
      this.page++
      this.list.push(...res.data.results)
      this.loading = false
      //   if (this.list.length >= res.data.total_count) {
      //     this.finished = true
      //   }
      // 以上两句可简化为：
      this.finished = this.list.length >= res.data.total_count
    }
  }
}
</script>

<style scoped></style>
